<?php

namespace App\Http\Controllers;

use App\Http\Resources\BaseResource;
use App\Http\Resources\ErrorResource;
use App\Repositories\Contracts\RulePolicyInterface;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use MakeRep\Controllers\BaseController;
use MakeRep\Resources\BaseCollection;
use MakeRep\Services\ParamService;

class RulePolicyController extends BaseController
{
    public function __construct(RulePolicyInterface $interface)
    {

        parent::__construct($interface);
    }

    /**
     * 上传携程政策
     * @param Request $request
     * @return BaseResource|ErrorResource
     */
    public function ajaxAdd(Request $request)
    {
        $res = $this->interface->create($request->all());
        if ($res === true) {

            return new BaseResource([]);
        }

        return new ErrorResource(['errmsg'=>$res]);
    }

    /**
     * 列表
     * @param Request $request
     * @return mixed
     */
    public function ajaxLists(Request $request){

        $pageSize = $request->page_size ? : 10;
        $orderBy = $request->order_by ? : 'desc';
        if ($request->platform && in_array($request->platform,$request->admin_platform)) {

            $where = ParamService::createCondition($request, isset($this->interface->fillable) ? $this->interface->fillable : []);
            $res = $this->interface->pageLists($where, '*', $pageSize, 'id', $orderBy);
            return new BaseCollection($res);
        }

        return new BaseCollection(new LengthAwarePaginator([],0,10));
    }

    /**
     * 政策编辑
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View|\MakeRep\Resources\BaseResource|\MakeRep\Resources\ErrorResource
     */
    public function update(Request $request)
    {

        //获取政策详情
        $res = $this->interface->policyInfo($request->all());
        if (is_array($res)) {
            return view('rulepolicy.update',['id'=>$request->id,'policy'=>$res[0]]);
        } else {

            abort(404);
        }

    }

    /**
     * 修改政策
     * @param Request $request
     * @return BaseResource|ErrorResource
     */
    public function ajaxUpdate(Request $request)
    {
        $res = $this->interface->ajaxUpdate($request->all());
        if ($res === true) {

            return new BaseResource([]);
        }

        return new ErrorResource(['errmsg'=>$res]);
    }


    /**
     * 简易更新政策
     * @param Request $request
     * @return BaseResource|ErrorResource
     */
    public function itemUpdate(Request $request){

        $res = $this->interface->itemUpdate($request->all());
        if ($res === true) {

            return new BaseResource([]);
        }

        return new ErrorResource(['errmsg'=>$res]);
    }

    /**
     * 删除政策
     * @param Request $request
     * @return BaseResource|ErrorResource
     */
    public function ajaxDelete(Request $request)
    {
        $res = $this->interface->deletePolicy($request->all());
        if ($res) {

            return new BaseResource([]);
        }

        return new ErrorResource(['errmsg'=>$res]);
    }
}